﻿define(function (require) {
    var datacontext = require('modules/dataservice');

    var vm = {
        orders: ko.observableArray(),
        error: ko.observable(),
        includeClosedFlag: ko.observable(false),
        activate: function () {
            var self = this;
            return getOrders(self.includeClosedFlag(), self.orders);
        },
        removeOrder: function (order) {
            vm.orders.remove(order);
            order.IsActive(false);
        },
        changeInclude: function () {
            var self = this;
            self.includeClosedFlag(!self.includeClosedFlag());
        },
        onNotification: function (params) {
            var self = this;
            if (params.DataType == 'Order') {
                getOrders(self.includeClosedFlag(), self.orders);
            }
        }
    };

    function getOrders(includeClosedFlag, orders) {
        return datacontext.getOrders(includeClosedFlag, orders);
    }

    vm.includeClosedFlag.subscribe(function (flag) {
        vm.includeClosedFlag(flag);
        datacontext.getOrders(vm.includeClosedFlag(), vm.orders);
    });

    return vm;
});